from collections import defaultdict
import os
import pandas as pd  
from openpyxl import Workbook  
import oss2
import configparser
import re  

    
def download_file_from_oss(object_key,save_path):  
    # 创建Auth实例  
    auth = oss2.Auth("LTAIjeF4dzpfFjUW", "pxFJtbFxfKT7XO0JifyZQIjshDQwNy")  
    # 创建Bucket实例  
    bucket = oss2.Bucket(auth, "http://oss-cn-beijing.aliyuncs.com", "hvyogo-pro")  
    try:  
        os.makedirs(os.path.dirname(save_path), exist_ok=True)  
        bucket.get_object_to_file(object_key, save_path)           
        return save_path  
  
    except OSError as e:  
        raise RuntimeError("OSS文件导出异常") from e  
    except Exception as e:  
        print(f"发生未知错误: {e}")  
        return ""



# 读取Excel文件  
excel_files=[
    "/Users/liyishan/Desktop/pythonDownLoad.xlsx"
]
data_map = defaultdict(list)  

for file_path in excel_files:  
    df = pd.read_excel(file_path)  
    for index, row in df.iterrows(): 
        cooperator_name = row['商户名称']  
        data_map[cooperator_name].append(row.to_dict())  # 将整行转换为字典并添加到列表中 

LOCALDIR = "/Users/liyishan/Desktop/pythonDownLoad"

for key,value_list in data_map.items():
    dirName = key
    directory_path = os.path.join(LOCALDIR, dirName)
    os.makedirs(directory_path)  
    for value_dict in value_list:
        fileName = value_dict['文件名称']+".pdf"
        fileUrl = value_dict['文件地址']
        download_file_from_oss(fileUrl,directory_path+"/"+fileName)



